package com.example.backend.controller;

import com.example.backend.common.ResultMessage;
import com.example.backend.entity.GouWuChe;
import com.example.backend.service.IGouWuCheService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/gouwuche")
public class GouWuCheController {
    @Resource
    private IGouWuCheService gouWuCheService;

    @PostMapping
    public ResultMessage addGouWuChe(@RequestBody GouWuChe gouWuChe){
        if(gouWuCheService.addGouWuChe(gouWuChe)){
            return ResultMessage.success(null,"新增成功");
        }
        return ResultMessage.error("新增失败");
    }

    @DeleteMapping("/{gwcid}")
    public ResultMessage deleteGouWuChe(@PathVariable("gwcid") Integer gwcid){
        if(gouWuCheService.deleteGouWuChe(gwcid)){
            return ResultMessage.success(null,"删除成功");
        }
        return ResultMessage.error("删除失败");
    }

    @PutMapping
    public ResultMessage updateGouWuChe(@RequestBody GouWuChe gouWuChe){
        if (gouWuCheService.selectGwc(gouWuChe.getYhid(), gouWuChe.getSjid()) == null){
            gouWuCheService.addGouWuChe(gouWuChe);
            return ResultMessage.success(gouWuChe,"新增成功");
        }else {
            gouWuCheService.updateGouWuChe(gouWuChe);
            return ResultMessage.success(gouWuChe,"更新成功");
        }
    }

    @GetMapping
    public ResultMessage queryGouWuChe(){
        return ResultMessage.success(gouWuCheService.queryGouWuChe(),"查询成功");
    }

    @GetMapping("/{gwcid}")
    public ResultMessage queryGouWuCheById(@PathVariable("gwcid") Integer gwcid){
        return ResultMessage.success(gouWuCheService.queryGouWuCheById(gwcid),"查询成功");
    }

    @GetMapping("/by-yhid/{yhid}")
    public ResultMessage queryGouWuCheByYHId(@PathVariable("yhid") Integer yhid) {
        return ResultMessage.success(gouWuCheService.queryGouWuCheByYHId(yhid), "查询成功");
    }

    @GetMapping("/by-cpid/{cpid}")
    public ResultMessage queryGouWuCheByCPId(@PathVariable("cpid") Integer cpid) {
        return ResultMessage.success(gouWuCheService.queryGouWuCheByCPId(cpid), "查询成功");
    }

    @GetMapping("/query/{yhid}/{sjid}")
    public ResultMessage selectGwc(@PathVariable("yhid") Integer yhid, @PathVariable("sjid") Integer sjid) {
        return ResultMessage.success(gouWuCheService.selectGwc(yhid, sjid), "查询成功");
    }
}
